<template>
  <div>
    <el-dialog
      :title="this.titleChangepwd"
      :visible.sync="centerDialogVisibleUserChangepwd"
      :close-on-click-modal="false"
      width="40%"
      :before-close="handleCloseChangepwd"
      center
    >
      <el-form
        ref="ruleFormUserChangepwd"
        :model="ruleFormUserChangepwd"
        :rules="rulesUserChangepwd"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="新密码" prop="sysUserPwd">
          <el-input v-model="ruleFormUserChangepwd.sysUserPwd" type="password" autocomplete="off" />
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
          <el-input v-model="ruleFormUserChangepwd.checkPass" type="password" autocomplete="off" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitFormUserChangepwd('ruleFormUserChangepwd')">立即创建</el-button>
          <el-button @click="resetFormUserChangepwd('ruleFormUserChangepwd')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import Changepwd from '@/api/axios/api/common/changepwd'
import Tip from '@/utils/tip'
export default {
  name: 'ChangePwd',
  props: {
    userChangepwd: {}
  },
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        if (this.ruleFormUserChangepwd.checkPass !== '') {
          this.$refs.ruleFormUserChangepwd.validateField('checkPass')
        }
        callback()
      }
    }
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.ruleFormUserChangepwd.sysUserPwd) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      centerDialogVisibleUserChangepwd: false,
      titleChangepwd: '',
      ruleFormUserChangepwd: {
        sysUserUuid: '',
        sysUserPwd: '',
        checkPass: ''
      },
      rulesUserChangepwd: {
        sysUserPwd: [
          { validator: validatePass, trigger: 'blur' }
        ],
        checkPass: [
          { validator: validatePass2, trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    userChangepwd: {
      deep: true,
      immediate: false,
      handler() {
        this.centerDialogVisibleUserChangepwd = this.userChangepwd.centerDialogVisibleUserChangepwd
        this.ruleFormUserChangepwd.sysUserUuid = this.userChangepwd.sysUserUuid
        this.titleChangepwd = '修改用户: ' + this.userChangepwd.titleChangepwd + ' 密码'
      }
    }
  },
  methods: {
    handleCloseChangepwd() {
      this.centerDialogVisibleUserChangepwd = false
      this.$emit('centerDialogVisibleUserChangepwd')
    },
    handleChange(value) {
    },
    submitFormUserChangepwd(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          Changepwd.changePwd(this.ruleFormUserChangepwd).then(res => {
            Tip.success(res.data)
            setTimeout(() => {
              this.$emit('centerDialogVisibleUserChangepwd', false)
            }, 2000)
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetFormUserChangepwd(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style scoped>

</style>
